The File Concatenation program, as the name suggests, concatenates the contents of a given number of files. We specify the files on the command line as a variable argument list. The output of the program will be a new file that is a sequential combination of all the input files.
In this application, we learn a new language feature (introduced in J2SE 5.0) called varargs, which are variable arguments to a method.
Program Code
The code for the File Concatenation program is given here:
import java.io.*;
public class Concatenate {
public static void concenateFile(String... fileName) { String str = null;
try (BufferedWriter writer = new BufferedWriter(new FileWriter( "CombinedFile.txt"));) { for (String name : fileName) { try (BufferedReader reader = new BufferedReader(new FileReader(
name));) { while ((str = reader.readLine()) != null) { writer.write(str); writer.newLine(); }
} catch (IOException e) {
System.out.println("Error reading/writing file"); }
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length < 0) {
System.out.println("Usage: java Concatenate file1 file2"); System.exit(0);
}
concenateFile(args);
System.out.println("Successfully created CombinedFile.txt"); }
}
- The File Concatenation program accepts a variable number of arguments on the command line. The arguments specify the files to be concatenated.
- First comes the content of the file, as specified by the first argument. To this the contents of the file specified by the second argument are appended, and so on. The resultant output is stored in a file named CombinedFile.txt.
- The original files remain unchanged. After checking the valid input on the command line, the main function calls the concenateFile method by sending it the variable list of arguments:
public static void concenateFile(String... fileName) {
- Note how this list is specified. The ellipsis indicates that the number of arguments is variable. Each argument is of type String. This varargs (variable arguments) feature allows a developer to declare that a method can take a variable number of parameters for a given argument. The varargs must be the last argument in the formal argument list. The compiler converts this list of variable arguments to an array. Inside the method, we iterate through this variable arguments list by using a foreach loop:
for (String name : fileName) {
- In each iteration, we open a FileReader on the specified file, read its contents a line at a time, and keep on writing to a previously created writer instance:
try (BufferedReader reader = new BufferedReader(new FileReader(name));) {
while ((str = reader.readLine()) != null) {
writer.write(str);
writer.newLine();
}
}
Note that we use the try-with-resources syntax of Java SE 7 to open the files. This reduces the exception-handling code substantially. Also, we do not have to worry about closing the resources.
Output:
A typical program run is shown here:
C:\360\io>java Concatenate MindStick1.txt MindStick2.txt MindStick3.txt
C:\360\io>
Note that the program does not output anything to the console. Instead, it creates a CombinedFile.txt file in the current folder. The newly created file will contain the concatenation of the three specified input files.
Sunil Singh
12-Apr-2017